# frozen_string_literal: true
class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  skip_before_action :check_auth, only:[:import]

  def index
    @users =
      if @current_editor.system?
        User.all.page(page).per(per)
      else
        @current_editor.village.users.page(page).per(per)
      end
  end

  # GET /users/1
  # GET /users/1.json
  def show; end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit; end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to users_path, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def import
    village = Village.find_or_create_by(
            name: params[:name],
            address: params[:address]
            )
    editor = Editor.find_or_create_by(
      name: params[:floor_name],
      telephone: params[:floor_telephone].strip,
      village: village
    )
    editor.add_role(:floor)
    building = Building.find_or_create_by(
       village: village,
       editor: editor,
       code: params[:code]
       )
    User.create(
      name: params[:user_name],
      village: village,
      building: building,
      id_card: params[:id_card],
      telephone: params[:telephone].strip,
      floor_code: params[:floor_code],
      car_code: params[:car_code],
      avatar: params[:avatar],
      address: params[:user_address],
      number: params[:number]
    )
    editor_estate = Editor.find_or_create_by(
      name: params[:estate_name],
      telephone: params[:estate_telephone].strip,
      village: village
    )
    editor_estate.add_role(:estate)
    editor_door = Editor.find_or_create_by(
      name: params[:door_name],
      telephone: params[:door_telephone].strip,
      village: village
    )
    editor_door.add_role(:door)
    render json: { code: true }
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_user
    @user = User.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def user_params
    params.require(:user).permit(:name, :floor_code, :telephone,:avatar)
  end
end
